﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using TouchRC.Logic;
using System.Drawing;

namespace TouchRC.BarManagers
{
    public class BarManager : FluidPanel
    {
        private List<FluidButton> m_Buttons = new List<FluidButton>();
        private bool m_IsVisible = false;

        void closeButton_Click(object sender, EventArgs e)
        {
            HideOptionsBar();
            TopBarManager topBar = new TopBarManager();
            topBar.ShowTopBar();
        }

        public BarManager(List<BarOption> options)
        {
            if (options.Count > 5)
                throw new Exception("Too many options, I can't handle that many!!");

            int screenWidth = (int)(AppSettings.ScreenResolution.Width / (ScaleFactor.Width));
            int buttonDim = (int)(AppSettings.ButtonDimensions * ScaleFactor.Width);

            int numberOfButtons = options.Count + 1;
            int margin = (int)(10 * ScaleFactor.Width);
            int xPos = margin;
            // Do static field here.

            FluidButton closeButton = new FluidButton(string.Empty, xPos, 5, buttonDim, buttonDim);
            closeButton.BackColor = Color.Empty;
            closeButton.Shape = ButtonShape.Flat;
            closeButton.Image = ImageFactory.GetBitmap(ImageTypes.Cross);
            closeButton.Click += new EventHandler(closeButton_Click);
            closeButton.Visible = true;

            m_Buttons.Add(closeButton);

            if (options.Count > 0)
            {
                int totalSpareSpace = screenWidth - (numberOfButtons * buttonDim) - (2 * margin);
                int buttonGap = totalSpareSpace / (numberOfButtons - 1);

                foreach (BarOption option in options)
                {
                    xPos += (buttonGap + buttonDim);
                    FluidButton button = new FluidButton(string.Empty, xPos, 5, buttonDim, buttonDim);
                    button.BackColor = Color.Empty;
                    button.Shape = ButtonShape.Flat;
                    button.Image = option.Image;
                    button.Click += new EventHandler(option.OnClick);
                    button.Visible = true;

                    m_Buttons.Add(button);
                }
            }

            foreach (FluidButton button in m_Buttons)
            {
                Controls.Add(button);
            }
        }

        protected override void InitControl()
        {
            base.InitControl();
            Visible = false;
            Bounds = new Rectangle(0, 0, AppSettings.ScreenResolution.Width, (AppSettings.ButtonDimensions + 10));
            BackColor = Color.Black;
            ForeColor = Color.White;
            GradientFill = true;
            GradientFillOffset = 24;
            EnableDoubleBuffer = true;
            
            
        }

        public void ShowOptionsBar()
        {
            if (!m_IsVisible)
            {
                base.ShowModal(ShowTransition.FromBottom);
                m_IsVisible = true;
            }
        }

        public void HideOptionsBar()
        {
            if (m_IsVisible)
            {
                base.Close(ShowTransition.FromBottom);
                m_IsVisible = false;
            }
        }

    }
}
